<?php
/**
 * @Project: None
 * @FileName: GameAttr.php
 * @FilePath: D:\thinkphp6\tp\app\controller\GameAttr.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-05 11:42:17
 */
namespace app\controller;
use think\Facade\Db;
use app\model\GameAttribute as GAttr;
use think\facade\Request;
use app\BaseController;
use app\Response;

class GameAttr extends BaseController{
    /**
     * [gameList 获取游戏属性列表]
     * @return [type] [description]
     */
    public function gameAttrList(){
        //静态获取参数
        //$param = Request::param();
        $param = $this->request->param();
        $page = isset($param['page'])?(int)$param['page']:1;
        $perpage = isset($param['perpage'])?(int)$param['perpage']:10;

        $game_id = isset($param['game_id'])?(int)$param['game_id']:1;

        $name = isset($param['name'])?(string)$param['name']:'';

        //筛选条件数组
        $where = [];

        if(!empty($name)){
            $where[] = [
                ['name', 'like', '%'.$name.'%']
            ];
        }

        // 查询数据 
        $list = Db::name('game_attribute')->where('game_id',$game_id)->where($where)->order('id' ,'asc')->paginate($perpage);
        //当前页数
        $currentPage = $list->currentPage();
        // 获取总记录数
        $total = $list->total();
        //最后页数
        $lastPage = $list->lastPage();
        //每页显示数
        $listRows = $list->listRows();
        //数据
        $data = $list->items();
        
        $result = array(
            'currentPage' => $currentPage,
            'total' => $total,
            'lastPage' => $lastPage,
            'listRows' => $listRows,
            'content' => $data
        );

        
        Response::show(200,'ok',$result);
    }

    /**
     * [edit 游戏属性编辑]
     * @return [type] [description]
     */
    public function edit(){
        $param = Request::param();
        $id = isset($param['id'])?(int)$param['id']:0;
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;

        if($id < 1 || $game_id < 1){
            $result = array('id'=>$id,'game_id'=>$game_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //游戏属性名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'游戏属性名称与英文简称不能为空');
        }

        //同一游戏内游戏属性不能重复
        $repeat = GAttr::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty() && $repeat->id != $id){
            Response::show(300,'游戏属性已存在');
        }

        //同一游戏内游戏属性英文简称也不能重复
        $repeat_en_name = GAttr::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty() && $repeat_en_name->id != $id){
            Response::show(300,'游戏属性英文简称已存在');
        }

        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';

        //根据id查找数据，并判断数据是否存在
        $role = GAttr::where('id',$id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'数据不存在');
        }
        //存在则修改
        $role->name = $name;
        $role->en_name = $en_name;
        $role->introduction = $introduction;
        $role->update_time = date('Y-m-d H:i:s');
        $role->save();
        Response::show(200,'修改成功');
    }
    /**
     * [addAttr 增加游戏属性]
     */
    public function addAttr(){
        $param = Request::param();
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;

        if($game_id < 1){
            $result = array('game_id'=>$game_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //游戏属性名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'游戏属性名称与英文简称不能为空');
        }

        //同一游戏内游戏属性不能重复
        $repeat = GAttr::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty()){
            Response::show(300,'游戏属性已存在');
        }

        //同一游戏内游戏属性英文简称也不能重复
        $repeat_en_name = GAttr::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty()){
            Response::show(300,'游戏属性英文简称已存在');
        }

        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';

        $attr = new GAttr;
        $attr->game_id = $game_id;
        $attr->name = $name;
        $attr->en_name = $en_name;
        $attr->introduction = $introduction;
        $attr->create_time = date('Y-m-d H:i:s');
        $attr->update_time = date('Y-m-d H:i:s');
        $attr->save();
        //获取自增ID
        $id = $attr->id;
        Response::show(200,'新增成功',$id);
    }
    /**
     * [delGameAttr 删除游戏属性]
     * @return [type] [description]
     */
    public function delGameAttr()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        //验证游戏属性是否存在
        $gameAttr = GAttr::where('id',$id)->findOrEmpty();
        if($gameAttr->isEmpty()){
            Response::show(404,'游戏属性不存在');
        }

        $del = $gameAttr->delete(); //返回bool值
        if($del){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }

        Response::show($code,$msg);
    }
}